/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * @author ZhangMaoren 
 * @since 2015年9月7日
 * @version 0.1
 */
package com.suredy.app.testItem.service;

import java.util.LinkedHashMap;
import java.util.List;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.suredy.app.testItem.model.TestItem;
import com.suredy.core.service.BaseSrvWithEntity;


/**
 * @author ZhangMaoren
 *
 */
@Service("TestItemSrv")
public class TestItemSrv extends BaseSrvWithEntity<TestItem>{
	@Override
	public DetachedCriteria getDc(TestItem t) {
		DetachedCriteria dc = super.getDc(t);
		if (t == null) {
			return dc;
		}
		if (!StringUtils.isBlank(t.getId())) {
			dc.add(Restrictions.eq("id", t.getId()));
		}
		
		if (!StringUtils.isBlank(t.getName())) {
//			dc.add(Restrictions.eq("name", t.getName()));
			dc.add(Restrictions.like("name", t.getName(), MatchMode.ANYWHERE));
		}

		if (!StringUtils.isBlank(t.getCode())) {
			dc.add(Restrictions.eq("code", t.getCode()));
//			dc.add(Restrictions.like("code", t.getCode(), MatchMode.ANYWHERE));
		}
		return dc;
	}
	
	public List<TestItem> getAll() {
		LinkedHashMap<String, Boolean> order = new LinkedHashMap<String, Boolean>();
		order.put("name", false);
		List<TestItem> pos = this.readByEntity(null, order);

		return pos;
	}
	
	public Long Count() {
		String ql = "SELECT COUNT(*) FROM TestItem T";
		Object ret = this.readSingleByQL(ql);
		return (Long) ret;
	}
	
	public List<TestItem> getAll(Integer page, Integer size) {
		LinkedHashMap<String, Boolean> order = new LinkedHashMap<String, Boolean>();
		order.put("name", false);
		List<TestItem> pos = this.readByEntity(null, page, size, order);

		return pos;
	}
	
	public TestItem getById(String id) {
		TestItem po = this.get(id);
		if (po == null) {
			return null;
		}
		return po;
	}
	
	public TestItem getByCode(String code) {
		if (StringUtils.isBlank(code)) {
			return null;
		}
		TestItem search = new TestItem();
		search.setName(code);
		TestItem entity = this.readSingleByEntity(search);

		return entity;
	}
	
	@Transactional
	public List<TestItem> getByName(String name) {
		if (StringUtils.isBlank(name)) {
			return null;
		}
		LinkedHashMap<String, Boolean> order = new LinkedHashMap<String, Boolean>();
		order.put("name", false);
		TestItem search = new TestItem();
		search.setName(name);
		List<TestItem> pos = this.readByEntity(search, order);
		return pos;
	}
}
